<?php
/**
 * Created by PhpStorm.
 * User: hongyeah
 * Date: 2015/10/23
 * Time: 12:57
 */

namespace Home\Controller;


use Think\Controller;

class UsersController extends Controller
{

    public function information(){
        $this->getUsers();
        if($_SESSION['status']!==1){
            $this->display("landon");
            return;
        }
        if($_SESSION['sex']==1||$_SESSION['sex']=='男'){
            $_SESSION['sex'] = '男';
        }else{
            $_SESSION['sex'] = '女';
        }
        echo $_SESSION;
        $this->assign('user',$_SESSION);
        $this->display();
    }

    /**
     *挂号模块
     */
    public function sendReg(){
        $person_id = I('post.person_id');
        $dot_id = I('post.id'); //医生ID
        $dot_name = I('post.name');
        $order_date = I('post.date');//预约日期
        $see_time = I('post.time/d');//上下午
        $type = I('post.type/d');//内外科
        $sick = I('post.sick');//病情

        if($type==3){
            $this->getRecords($person_id);
        }

        $where = array(
            'dot_id' => $dot_id,
            'order_date' => $order_date,
            'see_time' => $see_time
        );
        $where1 = array(
            'person_id' =>$person_id,
            'order_date' => $order_date,
            'see_time' => $see_time
        );
        $users_model = D('Users');
        if($users_model->getPersonRecord($where1)==true) {
            $Message = array(
                'messages'=>array()
            );
            $this->ajaxReturn($Message);
        }
        $person_name = $users_model->field('name')->where(array('person_id'=>$person_id))->find();
        $data = array(
            'person_id' =>$person_id,
            'person_name' =>$person_name,
            'dot_id' => $dot_id,
            'order_date' => $order_date,
            'see_time' => $see_time,
            'dot_name' =>$dot_name,
            'type'  =>$type,
            'sick'  =>$sick,
            'rank'  =>($users_model->getRank($where)+1),
            'see_date' =>date('y-m-d h:i:s',time()) ,
        );

        if(($users_model->arrangeRegs($data))==true){
            $where2['person_id'] = $person_id;
            $Message['messages']=$users_model->getAllRecord($where2);
            $this->ajaxReturn($Message);
        }else{
            $Message = array(
                'messages'=>array()
            );
            $this->ajaxReturn($Message);
        }
    }
    //121.42.195.176/hospital/index.php/Home/Users/edtPassword
    public function edtPassWord(){
        $person_id = I('person_id');
        $pass = I('pass');
        $word = I('word');
        $where['person_id'] = $person_id;
        $where['password'] = $pass;
        $users_model = D('Users');
        if(!$users_model->checkPassWord($where)){
            $data[0]['flag']=false;
            $Message['messages'] = $data;
            $this->ajaxReturn($Message);
        }
        $da['person_id'] = $person_id;
        $da['password'] = $word;
        $users_model->edtUserPassWord($da);
        $data[0]['flag']=true;
        $Message['messages'] = $data;
        $this->ajaxReturn($Message);

    }

    public function sendRegs(){
        $person_id = I('get.person_id');
        $dot_id = I('get.id'); //医生ID
        $dot_name = I('get.name');
        $order_date = I('get.date');//预约日期
        $see_time = I('get.time/d');//上下午
        $type = I('get.type/d');//内外科
        $sick = I('get.sick');//病情

        if($type==3){
            $this->getRecords($person_id);
        }

        $where = array(
            'dot_id' => $dot_id,
            'order_date' => $order_date,
            'see_time' => $see_time
        );
        $where1 = array(
            'person_id' =>$person_id,
            'order_date' => $order_date,
            'see_time' => $see_time
        );
        $users_model = D('Users');
        if($users_model->getPersonRecord($where1)==true) {
            $Message = array(
                'messages'=>array()
            );
            $this->ajaxReturn($Message);
        }
        $data = array(
            'person_id' =>$person_id,
            'dot_id' => $dot_id,
            'order_date' => $order_date,
            'see_time' => $see_time,
            'dot_name' =>$dot_name,
            'type'  =>$type,
            'sick'  =>$sick,
            'rank'  =>($users_model->getRank($where)+1),
            'see_date' =>date('y-m-d h:i:s',time()) ,
        );

        if(($users_model->arrangeRegs($data))==true){
            $where2['person_id'] = $person_id;
            $Message['messages']=$users_model->getAllRecord($where2);
            $this->ajaxReturn($Message);
        }else{
            $Message = array(
                'messages'=>array()
            );
            $this->ajaxReturn($Message);
        }
    }


    public function getRecords($person_id){
        $where['person_id'] = $person_id;
        $users_model = D('Users');
        $Message['messages']=$users_model->getAllRecord($where);
        $this->ajaxReturn($Message);
    }

    public function newregisters(){
        $this->display();
    }

    public function saveUsers(){
        $name = I('name');
        $cardnum = I('cardnum');
        $department = I('department');
        $major = I('major');
        $sex = I('sex');
        $data = array(
            'name'          => $name,
            'cardnum'       => $cardnum,
            'depardment'    => $department,
            'major'         => $major,
        );

        if($sex=='男') {
            $data['sex'] = 1;
        }else{
            $data['sex'] = 0;
        }

        $users_model = D('Users');

        $result = $users_model->edtUsers($data);

        $this->ajaxReturn($result);

    }

    public function getUsers(){
        $cardnum = $_GET['cardnum'];
        if(isset($cardnum)){
            $users_model = D('Users');
            $_SESSION = $users_model->getUsersByNum($cardnum);
            $_SESSION['status'] = 1;
        }
    }


    //121.42.195.176/hospital/index.php/Home/Users/getUser
    public function getUser(){
        $person_id = I('person_id');
        if(isset($person_id)){
            $users_model = D('Users');
            $data = $users_model->getUsersByID($person_id);
            if($data[0]['sex']==1){
                $data[0]['sex']='男';
            }else{
                $data[0]['sex']='女';
            }
            $message['messages'] = $data;
            $this->ajaxReturn($message);
        }
    }
    //121.42.195.176/hospital/index.php/Home/Users/edtUsers
    public function edtUsers(){
        $person_id = I('person_id');
        $name = I('name');
        $cardnum = I('number');
        $department = I('department');
        $major = I('major');
        $sex = I('sex');
        $phone = I('phone');
        $address = I('address');
        $data = array(
            'address'       => $address,
            'phone'         => $phone,
            'person_id'     => $person_id,
            'name'          => $name,
            'cardnum'       => $cardnum,
            'depardment'    => $department,
            'major'         => $major,
        );

        if($sex=='男') {
            $data['sex'] = 1;
        }else{
            $data['sex'] = 0;
        }

        $users_model = D('Users');

        $result = $users_model->edtUsersByID($data);
        if($result==true){
            $users_model = D('Users');
            $data = $users_model->getUsersByID($person_id);
            if($data[0]['sex']==1){
                $data[0]['sex']='男';
            }else{
                $data[0]['sex']='女';
            }
            $message['messages'] = $data;
        }else{
            $message['messages']=array();
        }
        $this->ajaxReturn($message);
    }

    public function loginByGet(){
        $cardnum = I('get.number');
        $password = I('get.password');
        $result = D('Users')->login($cardnum,$password);
        if($result->messages!=null)
        {
            $_SESSION['id'] = $result->messages[0]['person_id'];
        }
        if($result)
            $this->ajaxReturn($result);
        else
            $this->ajaxReturn(false);
    }

    public function loginByPost(){
        $cardnum = I('post.number');
        $password = I('post.password');
        $result = D('Users')->login($cardnum,$password);
        if($result->messages!=null)
        {
            $_SESSION['id'] = $result->messages[0]['person_id'];
        }
        if($result)
            $this->ajaxReturn($result);
        else
            $this->ajaxReturn(false);
    }



}